package testGUI;

import java.awt.Dimension;
import java.awt.Point;
import java.util.ArrayList;

import javax.swing.JLabel;

public class Move extends Thread {

	private CollisionControl collisionControl;
	
	private boolean threadPaused;
	private JLabel labelToMove;
	private String directionFlag;

	public Move(JLabel label, ArrayList<Point> stonePositions) {
		directionFlag = "";
		threadPaused = false;
		labelToMove = label;
		collisionControl = new CollisionControl(stonePositions, label);
	}

	@Override
	public void run() {
		while (true) {
			if (directionFlag.equals("RIGHT") && labelToMove.getLocation().getX() < 550 && collisionControl.noCollision(directionFlag)) {
				labelToMove.setLocation(new Double(labelToMove.getLocation()
						.getX()).intValue() + 2, new Double(labelToMove
						.getLocation().getY()).intValue());
			} else if (directionFlag.equals("LEFT") && labelToMove.getLocation().getX() > 0 && collisionControl.noCollision(directionFlag)) {
				labelToMove.setLocation(new Double(labelToMove.getLocation()
						.getX()).intValue() - 2, new Double(labelToMove
						.getLocation().getY()).intValue());
			} else if (directionFlag.equals("UP") && labelToMove.getLocation().getY() > 0 && collisionControl.noCollision(directionFlag)) {
				labelToMove.setLocation(new Double(labelToMove.getLocation()
						.getX()).intValue(), new Double(labelToMove
						.getLocation().getY()).intValue() - 2);
			} else if (directionFlag.equals("DOWN") && labelToMove.getLocation().getY() < 550 && collisionControl.noCollision(directionFlag)) {
				labelToMove.setLocation(new Double(labelToMove.getLocation()
						.getX()).intValue(), new Double(labelToMove
						.getLocation().getY()).intValue() + 2);
			}

			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			synchronized (this) {
				while (threadPaused) {
					try {
						wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}

	public void pause() {
		threadPaused = true;

	}

	public void proceed() {
		threadPaused = false;
		notify();
	}

	public void setMovementDirection(String direction) {
		directionFlag = direction;
	}
}
